package com.skydonkey.dao;

import com.github.pagehelper.Page;
import com.skydonkey.pojo.po.VideoComment;
import org.apache.ibatis.annotations.*;
import java.util.List;

@Mapper
public interface VideoCommentMapper {

    @Select("SELECT * FROM video_comments WHERE comment_id = #{commentId}")
    VideoComment findById(@Param("commentId") Long commentId);

    @Insert("INSERT INTO video_comments (video_id, uid, content) " +
            "VALUES (#{videoId}, #{uid}, #{content})")
    @Options(useGeneratedKeys = true, keyProperty = "commentId", keyColumn = "comment_id")
    int insert(VideoComment videoComment);

    @Update("UPDATE video_comments SET video_id = #{videoId}, uid = #{uid}, content = #{content}, " +
            "create_time = #{createTime}, sec_comment_count = #{secCommentCount} WHERE comment_id = #{commentId}")
    int update(VideoComment videoComment);

    @Delete("DELETE FROM video_comments WHERE comment_id = #{commentId}")
    int deleteById(@Param("commentId") Long commentId);

    @Select("SELECT * FROM video_comments WHERE video_id = #{videoId}")
    Page<VideoComment> findByVideoId(@Param("videoId") Long videoId);


    @Select("SELECT COUNT(*) AS total_comments " +
            "FROM (" +
            "    SELECT comment_id" +
            "    FROM video_comments" +
            "    WHERE video_id = #{videoId}" +
            "    UNION ALL" +
            "    SELECT vc.comment_id" +
            "    FROM video_comments vc" +
            "    JOIN video_sec_comments vsc ON vc.comment_id = vsc.comment_id" +
            "    WHERE vc.video_id = #{videoId}" +
            ") AS combined_comments;")
    Long getTotalCount(@Param("videoId") Long videoId);
}
